<?php

/**
 * DoctorContact form.
 *
 * @package    docplus.co.za
 * @subpackage form
 * @author     Fox Matuku
 * @version    SVN: $Id: sfDoctrineFormTemplate.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class DoctorContactForm extends ContactForm {

	public function configure() {
		unset(
			$this['created_at'], $this['updated_at']
		);

		$this->widgetSchema['user_id'] = new sfWidgetFormInputHidden();
		$this->widgetSchema['practice_name'] = new sfWidgetFormInputText(array(), array("autocomplete" => "off"));
		$this->widgetSchema['practice_number'] = new sfWidgetFormInputText(array(), array("autocomplete" => "off"));
		$this->widgetSchema['work_phone']->setAttributes(array("autocomplete" => "off"));
		$this->widgetSchema['mobile_phone']->setAttributes(array("autocomplete" => "off"));
		$this->widgetSchema['street_address'] = new sfWidgetFormInputText(array(), array("autocomplete" => "off"));
		$this->widgetSchema['post_code']->setAttributes(array("autocomplete" => "off"));
		$this->widgetSchema['area']->setAttributes(array("autocomplete" => "off"));
		$this->widgetSchema['city_id']->setOption("add_empty", "Please select a city")->setAttributes(array("class" => "select-box-small","autocomplete" => "off"));
		$this->widgetSchema['region_id']->setOption("add_empty", "Please select a region")->setAttributes(array("class" => "select-box-small","autocomplete" => "off"));
        $this->widgetSchema['send_sms'] = new sfWidgetFormInputCheckbox(array(), array('value' => "1"));
        $this->widgetSchema['send_sms']->setLabel('Preferably, send me a text message rather!');

		$this->validatorSchema['practice_name'] = new sfValidatorString(array('max_length' => 255, 'required' => true), array('required' => 'Your *practice name* is required.'));
		$this->validatorSchema['practice_number'] = new sfValidatorString(array('max_length' => 255, 'required' => true), array('required' => 'Your *practice number* is required.'));
		$this->validatorSchema['work_phone'] = new gfPhoneNumberValidator(array("required" => true), array("invalid" => "Your *work phone* is invalid.", "required" => "Your *work phone* is required."));
		$this->validatorSchema['mobile_phone'] = new gfPhoneNumberValidator(array("required" => true), array("invalid" => "Your *mobile phone* is invalid.", "required" => "Your *mobile phone* is required."));
		$this->validatorSchema['street_address'] = new sfValidatorString(array('max_length' => 255, 'required' => true), array('required' => 'Your *street address* is required.'));
		$this->validatorSchema['post_code'] = new sfValidatorString(array('max_length' => 255, 'required' => true), array('required' => 'Your *post code* is required.'));
		$this->validatorSchema['area'] = new sfValidatorString(array('max_length' => 255, 'required' => true), array('required' => 'Your *area* is required.'));
		$this->validatorSchema['city_id'] = new sfValidatorString(array('max_length' => 255, 'required' => true), array('required' => 'Your *city* is required.'));
		$this->validatorSchema['region_id'] = new sfValidatorString(array('max_length' => 255, 'required' => true), array('required' => 'Your *region* is required.'));
		$this->validatorSchema['send_sms'] = new sfValidatorString(array('required' => false));
	}

	/**
	 * Override the save method to save the merged doctor form.
	 */
	public function save($con = null) {
		$this->updateDoctor();
		return parent::save($con);
	}

	/**
	 * Updates the doctor merged form
	 */
	protected function updateDoctor() {
		$doctor = DoctorTable::getInstance()->findOneByUserId($this->values['user_id']);
		if (is_object($doctor)) {
			$doctor->setPracticeName($this->values['practice_name']);
			$doctor->setPracticeNumber($this->values['practice_number']);
			$doctor->save();

			unset($this->values['practice_name'], $this->values['practice_number']);
            
            // save doctor's location
            $this->saveDoctorLocation($doctor);
		}
	}

	/**
	 * Save the doctor's first location
	 */
	protected function saveDoctorLocation($doctor) {        
        $doctorLocation = new DoctorLocation();
        $doctorLocation->setDoctorId($doctor->getId());
        $doctorLocation->setPhone($this->values['work_phone']);
        $doctorLocation->setStreetAddress($this->values['street_address']);
        $doctorLocation->setPostCode($this->values['post_code']);
        $doctorLocation->setArea($this->values['area']);
        $doctorLocation->setCityId($this->values['city_id']);
        $doctorLocation->setRegionId($this->values['region_id']);
        $doctorLocation->save();
	}
}
